package com.zhaolixiang.slidetabview

import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.widget.FrameLayout
import android.widget.RelativeLayout
import android.widget.Scroller
import android.support.v4.view.ViewConfigurationCompat
import android.view.ViewConfiguration
import android.view.animation.LinearInterpolator


/**
 * Created by Administrator on 2018/1/10.
 */
class MyFrameLayout:RelativeLayout {
    private val MaxHeight=200f
    constructor(context: Context):super(context)
    constructor(context: Context,attributeSet: AttributeSet):super(context,attributeSet)

    /**
     * 用于完成滚动操作的实例
     */
    private val mScroller: Scroller=Scroller(context);

    //滑动的y坐标值
    private var perviousY = 0f
    /**
     * 滚动方向：direction
     */
    enum class Direction {
        LEFT, RIGHT, NON
    }
    //初始化，未设置
    private var direction = Direction.NON
    //每次移动的高度
    private var dy=0f
    //每次移动完成后的高度:0f,或者MaxHeight
    private var dyFinish=0f
    /**
     * 判定为拖动的最小移动像素数
     */
    private var mTouchSlop: Int = 0

    init {
        val configuration = ViewConfiguration.get(context)
        // 获取TouchSlop值
        mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration)
    }
    override fun onTouchEvent(ev: MotionEvent): Boolean {
        Log.e("下拉","${ev.y}---${ev.action}---$perviousY")
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                //按下开始滑动
                perviousY = ev.y
            }
            MotionEvent.ACTION_MOVE -> {
                //触摸移动中
                if (ev.y > perviousY) {
                    //想下移动
                    direction = Direction.RIGHT
                } else if (ev.y < perviousY) {
                    //向上移动
                    direction = Direction.LEFT
                } else {
                    direction = Direction.NON
                }
                dy+=ev.y-perviousY
                if(dy>MaxHeight){
                    dy=MaxHeight
                }
                if(dy<0f){
                    dy=0f
                }
                // 第二步，调用startScroll()方法来初始化滚动数据并刷新界面
                //mScroller.startScroll(0, scrollY, 0, dy.toInt());
                val params=layoutParams as RelativeLayout.LayoutParams
                params.topMargin=dy.toInt()
                layoutParams=params
            }
            MotionEvent.ACTION_UP -> {
                //手指离开时
                if(dy>dyFinish){
                    //需要完全展示
                    startAnimation(dy.toInt(),MaxHeight.toInt())
                    dyFinish=MaxHeight
                }else if(dy<dyFinish){
                    //需要隐藏
                    startAnimation(dy.toInt(),0)
                    dyFinish=0f
                }

            }
        }
        return true
        //return super.onTouchEvent(ev)
    }

    private fun startAnimation(start:Int,end:Int){
        val valueAnimator=ValueAnimator.ofInt(start,end)
        valueAnimator.duration=500
        valueAnimator.interpolator=LinearInterpolator()
        valueAnimator.addUpdateListener {
            animator->
            dy=animator.animatedValue.toString().toInt().toFloat()
            val params=layoutParams as RelativeLayout.LayoutParams
            params.topMargin=dy.toInt()
            layoutParams=params
        }
        valueAnimator.start()
    }

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        Log.e("下拉","onInterceptTouchEvent:${ev.y}---${ev.action}---$perviousY")
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                perviousY = ev.y
            }
            MotionEvent.ACTION_MOVE -> {

                val diff = Math.abs(ev.y- perviousY)

                // 当手指拖动值大于TouchSlop值时，认为应该进行滚动，拦截子控件的事件
                if (diff >mTouchSlop) {
                    perviousY = ev.y
                    return true
                }
            }
        }
        return super.onInterceptTouchEvent(ev)
    }

}